<template>
  <view class="index-list">
    <view class="loading" v-if="loading">
      <u-loading></u-loading>
      <text class="text">品牌加载中...</text>
    </view>
    <scroll-view class="scroll" scroll-y v-if="indexList.length > 0" :scroll-into-view="anchor" scroll-with-animation>
      <view class="item" v-for="(item, index) in indexList" :key="index" :id="`anchor-${item.initial}`">
        <view class="index-anchor"><text>{{item.initial}}</text></view>
        <view class="index-item">
          <view class="children-item" v-for="(i, k) in item.data" :key="k" @click="onClickBrand(i)">
            <text>{{i.brand}}</text>
          </view>
        </view>
      </view>
    </scroll-view>
    
    <view class="index-bar" v-if="indexList.length > 0">
      <text class="text" v-for="(item, index) in indexList" :key="index" @click="onClickAnchor(item.initial)">{{item.initial}}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      indexList: ['A', 'B'],
      loading: true,
      anchor: 'anchor-A'
    }
  },
  created() {
    this.getCarBrandList();
  },
  methods: {
    async getCarBrandList() {
      this.loading = true
      try {
        const res = await this.$api.common.GetCarBrandList()
        this.indexList = res.data
        this.loading = false
      } catch (err) {
        console.log(err)
      }
    },
    // 点击索引
    onClickAnchor(data) {
      this.anchor = `anchor-${data}`;
    },
    // 点击品牌
    onClickBrand(data) {
      this.$emit('onClickBrand', data);
    }
  }
}
</script>

<style lang="scss" scoped>
.index-list{
  background-color: #fff;
  height: calc(100vh - 80rpx);
  overflow: hidden;
  position: relative;
  .loading{
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26rpx;
    color: #999;
    .text{
      margin-left: 10rpx;
    }
  }
  .scroll{
    height: 100%;
    .item{
      .index-anchor{
        box-sizing: border-box;
        padding: 14rpx 24rpx;
        color: #606266;
        width: 100%;
        font-weight: 500;
        font-size: 28rpx;
        line-height: 1.2;
        background-color: #f5f5f5;
      }
      .index-item{
        .children-item{
          display: flex;
          box-sizing: border-box;
          width: 100%;
          padding: 20rpx 24rpx;
          overflow: hidden;
          color: #606266;
          font-size: 28rpx;
          border-top: 1px solid #e4e7ed;
          &:first-child{
            border-top: none;
          }
        }
      }
    }
  }
  .index-bar{
    display: flex;
    flex-direction: column;
    position: absolute;
    right: 25rpx;
    top: 50%;
    transform: translateY(-50%);
    font-size: 24rpx;
    text-align: center;
    .text{
      padding: 0 5rpx;
    }
  }
}
</style>
